<?php

namespace App\Http\Controllers\Home;

use App\Http\Controllers\Core\BaseController;
use App\Models\Category;
use App\Models\District;
use App\Models\Job;
use Illuminate\Support\Facades\Session;

class IndexController extends BaseController
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        //$this->middleware('auth');
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //消防类推荐工作
        //找到最新的6条记录
        $recommend1 = Job::limit(6)->orderBy('create_time','DESC')->get();

        $recommend2 = Job::limit(6)->orderBy('click','DESC')->get();
        $category = Category::select('id', 'pid', 'name')->where(['alias' => '_trade'])->orderBy('id', 'desc')->get()->toArray();
        $categoryCache = Category::category_cache();
        $data = [
            'recommend1' => $recommend1,
            'recommend2' => $recommend2,
            'cache' => $categoryCache,
            'category' => json_encode($category)
        ];
        return view('home.index.index', $data);
    }

    function list_to_tree($list, $pk = 'id', $pid = 'pid', $child = '_child', $root = 0)
    {
        // 创建Tree
        $tree = array();
        if (is_array($list)) {
            // 创建基于主键的数组引用
            $refer = array();
            foreach ($list as $key => $data) {
                $refer[$data[$pk]] =& $list[$key];
            }
            foreach ($list as $key => $data) {
                // 判断是否存在parent
                $parentId = $data[$pid];
                if ($root == $parentId) {
                    $tree[$data['id']] =& $list[$key];
                } else {
                    if (isset($refer[$parentId])) {
                        $parent =& $refer[$parentId];
                        $parent[$child][$data['id']] =& $list[$key];
                    }
                }
            }
        }
        return $tree;
    }

    /**
     * 验证是否是企业用户
     */
    public function isCompany()
    {
        $member = Session::get('member');

        if (!$member) {
            return $this->jsonResponse(1, '请登陆后再进行操作');
        }
        if ($member->type != 1) {
            return $this->jsonResponse(2, '只有企业用户可以下载简历');
        }
        return $this->jsonResponse(0, '成功',['html'=>url('resume/doc')]);
    }
}